﻿using System;
using System.Web;

namespace AdverseEventReportSystem.Utils {
	/// <summary>
	/// Cookie帮助类
	/// </summary>
	public class CookieHelper {
		/// <summary>
		/// 写cookie值
		/// </summary>
		/// <param name="cookieName">名称</param>
		/// <param name="cookieValue">值</param>
		public static void WriteCookie(string cookieName, string cookieValue) {
			HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
			if (cookie == null) {
				cookie = new HttpCookie(cookieName);
			}
			cookie.Value = cookieValue;
			HttpContext.Current.Response.AppendCookie(cookie);

		}
		/// <summary>
		/// 写cookie值
		/// </summary>
		/// <param name="cookieName">名称</param>
		/// <param name="cookieValue">值</param>
		/// <param name="expires">过期时间(分钟)</param>
		public static void WriteCookie(string cookieName, string cookieValue, int expires) {
			HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
			if (cookie == null) {
				cookie = new HttpCookie(cookieName);
			}
			cookie.Value = cookieValue;
			cookie.Expires = DateTime.Now.AddMinutes(expires);
			HttpContext.Current.Response.AppendCookie(cookie);

		}
		/// <summary>
		/// 读cookie值
		/// </summary>
		/// <param name="cookieName">名称</param>
		/// <returns>cookie值</returns>
		public static string GetCookie(string cookieName) {
			if (HttpContext.Current.Request.Cookies[cookieName] != null) {
				return HttpContext.Current.Request.Cookies[cookieName].Value.ToString();
			}
			return "";
		}
		/// <summary>
		/// 删除cookie
		/// </summary>
		/// <param name="cookieName">名称</param>
		public static void DeleteCookie(string cookieName) {
			HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
			if (cookie != null) {
				HttpContext.Current.Response.Cookies.Remove(cookieName);
			}
		}
	}
}
